%% Calculate w_hat

function [w_hat_new] = LabourMarketConditionCESOlig_fun(pi_l_f0, ...
            pi_l0,pi_l_f1,pi_l1,X_mnj,X_hat_mnj,...
            P_hat_n,pi_nkjf0,pi_nkjf1,pi_nkj0,pi_nkj1)
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Author: Produced for JdG,AL,IM by Christopher Evans at UPF 
%
% Calculates the labour market condition used for updating the guess of 
% the wage
%
% NEW: For all code, we will change the subscripts to match those in
% master_notes3.pdf and the draft. I.e., we will change to {mn,ij} pairs,
% which represent country pair {mn} and sector pair {ij}. Global variables 
% changed and slight modification to file structure for calling up data.
% We also get rid of gamma_type variable. What previously _2.csv. files
% slightly as well as adjusting in our new directory.
%
% Compared to C-D, we now also feed in updated next period values of the
% labor and intermediate shares calculated in TradeShare function.
%
% Last Updated: 31/01/2019 by JDG
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

global alphaT rhoT lambdaT etaT rho sigma lambda eta varphi ...
    tol maxit Pi_hat_n D_hat_n Xi_hat_mnjf Xi_hat_mnj a_hat_f a_hat ...
    N J FI_J france countrysecD firmsecD_sorted start start_sorted ...
    sum_by_country_dummy ISO ... 
    sL_n sPi_n sD_n alpha_WIOD_j alpha_WIOD_francej labour_share_PWT

%% Sector level

% sum_k_X_nkj: Sums the expenditure matrix by importers

sum_k_X_nkj=sum(X_mnj,2);

% pil0_pi0_sum_k_X_nk: Multplying by period 0 export and labor shares and
%                      rhos by sector

rho_nj=repmat(rho,[N 1]);
rho_njm=repmat(rho_nj,[1 N]);

%pil0_pi0_sum_k_X_nkj=((rho_nj-1)./rho_nj).*pi_l0.*pi_nkj0.*sum_k_X_nkj;

pil0_pi0_X_nkj=((rho_njm-1)./rho_njm).*repmat(pi_l0,[1 N]).*pi_nkj0.*X_mnj;
sum_k_pil0_pi0_X_nkj=sum((((rho_njm-1)./rho_njm).*repmat(pi_l0,[1 N]).*pi_nkj0.*X_mnj),2);

%sum_j_pil0_pi0_sum_k_X_nkj: Loop through countries i, to sum over sector j

for i=1:N
   sum_j_sum_k_pil0_pi0_X_nkj(1,i)=...
      sum(sum_k_pil0_pi0_X_nkj(start(i):start(i+1)-1,1),1);
end

% Denominator: Calculates the denominator, which is derived from rearranging
%              the labour market condition equation 

DEN=P_hat_n.^(1/(1-varphi)).*sum_j_sum_k_pil0_pi0_X_nkj;

% pil0_pi0_X_nkj_plhat_pi_nkjhat Multiply pil0_pi0_X_nkj by the change 
% in labor demand, for each sector origin and destination
pil0_pi0_X_nkj_pilhat_pi_nkjhat = pil0_pi0_X_nkj.*repmat(pi_l1,[1,N])./ ...
    repmat(pi_l0,[1,N]).*pi_nkj1./pi_nkj0.*X_hat_mnj;
pil0_pi0_X_nkj_pilhat_pi_nkjhat(isnan(pil0_pi0_X_nkj_pilhat_pi_nkjhat))=0;

% sum_k_pil0_pi0_X_nkj_pilhat_pi_nkjhat: Sum over destination countries
sum_k_pil0_pi0_X_nkj_pilhat_pi_nkjhat=sum(pil0_pi0_X_nkj_pilhat_pi_nkjhat,2);

% sum_kj_pil0_pi0_X_nkj_pilhat_pi_nkjhat: Sum over destination countries
for i=1:N
   NUM(1,i)=...
      sum(sum_k_pil0_pi0_X_nkj_pilhat_pi_nkjhat(start(i):start(i+1)-1,1),1);
end
   
% w_hat_new: initialising the new wage JDG: NEEDED?

% w_hat_new=zeros(1,N);

% Rearranging equation (6) to produce the new wage guess. Takes the
% numerator and denominator from above and takes them to the power of varphi

w_hat_new=(NUM./DEN).^((varphi-1)/varphi);

%% Firm level

% X_mnjf: Expanding the expenditure matrix X so it can fit into the
%          firm data

 X_mnjf= firmsecD_sorted...
                    *X_mnj(start(france):start(france+1)-1,:);
                
% X_hat_mnjf: Expanding the expenditure hat matrix X so it can fit into the
%              firm data               

 X_hat_mnjf=firmsecD_sorted...
                *X_hat_mnj(start(france):start(france+1)-1,:);

% pilf1_pif1_pi_X_f: Multiplying the firm export and labor shares and rho
repmat_sigma = repmat(firmsecD_sorted * sigma,[1,N]);
repmat_rho = repmat(firmsecD_sorted * rho,[1,N]);
mu_mnjf0 = repmat_sigma.*repmat_rho./(repmat_sigma.*(repmat_rho-1)-(repmat_rho-repmat_sigma).*pi_nkjf0);
mu_mnjf1 = repmat_sigma.*repmat_rho./(repmat_sigma.*(repmat_rho-1)-(repmat_rho-repmat_sigma).*pi_nkjf1);         
pilf0_pif0_X_f=repmat(pi_l_f0,[1 N]).*pi_nkjf0.*X_mnjf./mu_mnjf0;
           
% pi_hat_nkjf: Change in export share for french firms

    pi_hat_nkjf = ones(FI_J,N);
    pi_hat_nkjf(pi_nkjf0==0) = 1;
    pi_hat_nkjf(pi_nkjf0~=0) = pi_nkjf1(pi_nkjf0~=0)./pi_nkjf0(pi_nkjf0~=0);
    mu_hat_nkjf = ones(FI_J,N);
    mu_hat_nkjf = mu_mnjf1./mu_mnjf0;

    % pi_hat_l_f: change in labor share for french firms
    
    pi_hat_l_f = pi_l_f1./pi_l_f0;
    pi_hat_l_f(pi_l_f0==0) = 1;
    
% Numerator: Numerator of the labor market condition (numberator if we
%            isolate wages on one side

% NUM_f: Numberator for the french firms labor market condition.
%              Calculated by multiplying the term outside of the square 
%              brackets by the change in exports of firms.

NUM_f=pilf0_pif0_X_f.*repmat(pi_hat_l_f,[1,N]).*pi_hat_nkjf.*X_hat_mnjf./mu_hat_nkjf;

% sum_NUM_f: Summing by countries k, sectors j and firms f, this
%                  should then give us one value for the numerator

sum_NUM_f=sum(sum(NUM_f,2),1);

% Denominator: Denominator once we isolate wage hat

%DEN_f=pilf0_pif0_X_f;

% sum_DEN_f: Summing over countries k, sectors j and firms f, this
%                    will give us one value for the denominator for France

%sum_DEN_f=sum(sum(pilf0_pif0_X_f,2),1);

% P_hat_sum_DEN_f: Multiplying the denominator by the price hat 

P_hat_sum_DEN_f=P_hat_n(france).^(1/(1-varphi)).*sum(sum(pilf0_pif0_X_f,2),1);

% wage_hat_france: Wage change for france 

wage_hat_france= (sum_NUM_f./P_hat_sum_DEN_f)...
                                        .^((varphi-1)/varphi);

% w_hat_new: Replacing the change in wage done at the sector level for
%            france with the one done using firm data

w_hat_new(france)=wage_hat_france;
                                    
%% Normalizing by French wages

% Normalizing the wage hat by the wage hat of france, france is the 15th
% country on the list

w_hat_new=w_hat_new./w_hat_new(france);

end

